#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <vector>
#include "task.hpp"

using namespace std;
#define max_size 1024

int main()
{
    //将标准输入设定为非阻塞
    int status = fcntl(0, F_GETFL);//获取文件状态标记
    if (status < 0)
    {
        perror("fcntl error");
        exit(1);
    }
    fcntl(0, F_SETFL, status | O_NONBLOCK);//设置为非阻塞

    char buff[max_size];
    while(true)
    {
        cout << ">>>";
        fflush(stdout);
        int ret = read(0, buff, max_size - 1);
        if (ret > 0)
        {
            buff[ret - 1] = 0;
            cout << "echo:" << buff << endl;
        }
        if (ret == 0)
        {
            cout << "read end" << endl;
            break;
        }
        if (ret < 0)
        {
            for (auto e : task::get_task()._vtask)
            {
                e();
            }
        }
        sleep(1);
    }

    return 0;
}